const Com = require("../gmoon/base25DCom");
const Coms = require("../gmoon/baseCom");
cc.Class({
    extends: Com,

    properties: {
    },

    ctor(){
        this.setAttr("updateOnOff" , false );
    },

    onLoad () {
        this.node.zIndex = 10;
        this._animates = {};
    },

    start () {
        this.setAttr("_rt" , 0);
        $T.playHuxi( this.node );
    },

    run( stpos ){
        this.setAttr("_rt" , -$T.random(100 , 80) );
        this.setAttr("_inpanle" , false);

        this.node.opacity = 0;
        this.setAttr("HP", 100);
        
        let yr = $T.random(1 , 3 )/100,
            xr = $T.random(5 , 95)/100 ,
            zr = $T.random(10 , 80)/100;

        if(stpos){
            this.node.opacity = 180;
            this.node.setPosition( stpos );
            //this.setAttr("_inpanle" , true);
            this.emitRandom();
        }else{
            this.node.opacity = 180;
            this.setPosition25D(xr,0,zr);
            //console.log( "%s,%s,%s" , this.getAttr("xr") , this.getAttr("yr") ,this.getAttr("zr")  )
        }
        this.setAttr("updateOnOff" , true );
        this.playAnimates();
    },

    reset( id ){
        this.setAttr("_vid" , id ? id : $T.random(1, 99999));
        this.setAttr("_reuse" , true );
        this.setAttr("_rt" , 0);
        this.setAttr("_fx" , 0);
        let rigibody = this.node.getComponent(cc.RigidBody);
        rigibody.linearVelocity = cc.v2(0,0);
        this.setAttr("updateOnOff" , false );
        this.childNode( this._vinf.van.n ).active = false;
        return this;
    },

    reuse( id ){
        this.setAttr("_vid" , id ? id : $T.random(1, 99999));
        this.setAttr("_reuse" , true );
        this.setAttr("_rt" , -$T.random(60 , 120) );
        this.setAttr("_fx" , 0);
        let rigibody = this.node.getComponent(cc.RigidBody);
        rigibody.linearVelocity = cc.v2(0,0);
        this.setAttr("updateOnOff" , false );
        this.childNode( this._vinf.van.n ).active = false;

        return this;
    },

    unuse(){
        const pool = $G.get("pool-virus");
        this.setAttr("_booming" , false );
        this.setAttr("updateOnOff" , false );
        $G.get("viruspanel").removeVirus( this.getAttr("_vid") );
        this.setAttr("_unuse" , true );

        return this;
    },

    setBaseInfo(){
        return this;
    },

    emitRandom(){

        let ti = $T.random(1,12)-1;
        let rotation = ti*30;//$T.random(60 , 120);

        let rigibody = this.node.getComponent(cc.RigidBody);
        // rigibody.active = true;
        rigibody.gravityScale = 0;
        this.node.rotation = rotation;

        var dx = 1200 * Math.cos( $T.toRota(rotation) );
        var dy = 1200 * Math.sin( $T.toRota(rotation) );
    //cc.Vec2
        let posss = cc.v2(dx,dy);//e.touch.getLocation();
        let velocity =  posss.normalize().mulSelf( 1600 );
        rigibody.linearVelocity = velocity;

        this.setAttr("_emiting" , {posss : posss , v : 1200});
    },

    playAnimates( flag ){
        flag = $T.selectValue(flag , true);
        if(this.getAttr("updateOnOff") && flag){
            this._animates.he = 
            cc.tween( this.childNode("he"))
            .to(0 , {rotation:0})
            .to( $T.random(8,12), {rotation: $T.randomsign() * 360})
            .repeatForever()
            .start();
        }else if(!flag){
            this._animates.he && this._animates.he.stop();
            this._animates.he = null;
        }
    },

    onPosition( possize ){
        let cur = {w:this.node.width,h:this.node.height},
            to  = possize.size;
        let bvw = to.w/cur.w , bvh = to.h/cur.h ;
        let bi = this.childNode('bi') ,
            he = this.childNode('he') ,
            ht = this.childNode('heart'),
            gn = this.childNode('guan')
        $T.setNodeSize( bi , [bi.width*bvw ,] );
        $T.setNodeSize( he , [he.width*bvw ,] );
        $T.setNodeSize( ht , [ht.width*bvw ,] );
        $T.setNodeSize( gn , [gn.width*bvw ,] );
    },

    lateUpdate(){
        if( this.getAttr("_unuse" )){
            const pool = $G.get("pool-virus");
            pool.put( this.node );
            
            this.setAttr("_unuse" , false );
        }
    },

    update (dt) {
        //console.log(dt);
        if( this.getAttr("_unuse" ) ) return;
        if(this.getAttr("_reuse")) {
            // console.log( this.node.getPosition() )
            this.node.parent = $G.get("viruspanel").node;
            // this.node.setPosition(cc.v2($T.random(0 , 300) , 300));
            // this.node.x = 0;//$T.random(0 , 300);
            // this.node.y = $T.random(0 , 600);;
            // console.log( this.node.getPosition() )
            this.setAttr("_reuse" , false );
            //return;
            this.node.__my = 300;
        }

        // if( this.getAttr("updateOnOff") && $G.get("running") ){
        //     let yr = this.getAttr("yr"),
        //         xr = this.getAttr("xr"),
        //         zr = this.getAttr("zr");

        //         if(this.node.__my ) { this.node.__my -= dt*60 }
        //     // //if(yr<.7){
        //     //     //yr = yr>.6 ? (yr+Math.cos(dt)) : (yr+dt*.1);
        //     //     yr = yr + Math.pow(Math.asin(1/60),2)*3// Math.asin( dt )/20//Math.cos(dt)/1000;
        //     //     let cur = this.node.getPosition();


        //         //this.node.y = this.node.y-dt*60;
        //         // this.node.setPosition(cc.v2(cur.x ,this.node.__my))
        //         this.setPosition25D(xr,yr,zr);
        //     //}else{
        //     //    this.boom();
        //     //}
        // }
        // return ;
        this.__accDt = this.__accDt || 0.01;
        this.__accDt += dt;

        let emiting = this.getAttr("_emiting");
        if( $T.isType(emiting , "Number") ){
            //this.setAttr("_emiting" , emiting+dt);
            //emiting += dt*40;
        }

        let inPanel = ()=>{
            const virusbox = this.node.parent;
            const hw = this.node.width/2;
            const xlen = [ -virusbox.width/2 , virusbox.width/2 ],
                  ylen = [ -virusbox.height/2 , virusbox.height/2 ];
            const minePos = this.node.getPosition();

            let re = "in";
            if(minePos.x-hw < xlen[0]){
                re = "l";
            }else if( minePos.x+hw > xlen[1] ){
                re = "r";
            }else if( minePos.y-hw < ylen[0]  ){
                re = "b";
            }else if( minePos.y+2*hw > ylen[1]  ){
                re = "t";
            }
            return re;
        }

        if( this.getAttr("updateOnOff") && $G.get("running") ){
            if( !this.getAttr("_fx") ){
                this.setAttr("_fx" , $T.random(1,10)>5?-1:1)
            }
            
            const isIn = inPanel();
            let rt = this.getAttr("_rt")/*this.node.rotation*/ , 
                rotation = rt;

            if( isIn=="l" ){
                if(this.getAttr("_inpanle") ){
                    rt = 90 + rt;
                    this.setAttr("_fx" , this.getAttr("_fx")*-1);
                    rotation =  rt + (dt * 20*this.getAttr("_fx"));
                }else{
                    this.setAttr("_inpanle" , true);
                }
            }else if( isIn=="r" ){
                if(this.getAttr("_inpanle") ){
                    rt = 90 + rt;
                    this.setAttr("_fx" , this.getAttr("_fx")*-1);
                    rotation =  rt + (dt * 20*this.getAttr("_fx"));
                }else{
                    this.setAttr("_inpanle" , true);
                }
            }else if( isIn=="t" ){
                if(this.getAttr("_inpanle") ){
                    rt = 90 - rt;
                    this.setAttr("_fx" , this.getAttr("_fx")*-1);
                    rotation =  rt + (dt * 20*this.getAttr("_fx"));
                }else{
                    this.setAttr("_inpanle" , true);
                }
            }else if( isIn=="b" ){
                if(this.getAttr("_inpanle") ){
                    rt = 90 + rt;
                    this.setAttr("_fx" , this.getAttr("_fx")*-1);
                    rotation =  rt + (dt * 20*this.getAttr("_fx"));
                }else{
                    this.setAttr("_inpanle" , true);
                }
            }else{
                if( !this.getAttr("_chgRt") ){
                    this.setAttr("_chgRt" , $T.random(8 , 35)/10);
                }
                this.setAttr("_inpanle" , true);
                rt = rt>360? rt - 360 : (rt<-360 ? rt+360 : rt);
                rotation =  rt + (dt * 20*this.getAttr("_fx"));

                if(this.__accDt >= this.getAttr("_chgRt")){
                    this.__accDt = 0.01;
                    this.setAttr("_fx" , this.getAttr("_fx")*-1);
                    this.setAttr("_chgRt" , 0 );
                }
            }

            if( !this.getAttr("_inpanle") ){

                let yr = this.getAttr("yr"),
                    xr = this.getAttr("xr"),
                    zr = this.getAttr("zr");
                //console.log(yr);
                if(yr<.7){
                    //yr = yr>.6 ? (yr+Math.cos(dt)) : (yr+dt*.1);
                    yr = yr + Math.pow(Math.asin(1/60),2)*3// Math.asin( dt )/20//Math.cos(dt)/1000;
    
                    this.setPosition25D(xr,yr,zr);
                }else{
                    this.boom();
                }

                return;
            }else if( emiting ){
                let rigibody = this.node.getComponent(cc.RigidBody);
                if( emiting.v>=1 ){
                    // let posss = cc.v2(dx,dy);//e.touch.getLocation();
                    emiting.v = emiting.v/20;
                    // let velocity =  emiting.posss.normalize().mulSelf( emiting.v );
                    // rigibody.linearVelocity = velocity;

                    this.setAttr("_emiting" , emiting );

                }else{
                    this.setAttr("_emiting" , null );
                    // rigibody.active = true;
                    rigibody.gravityScale = 0;
                }
                return;
            }else{
              this.setAttr("_rt" , rotation);        
            }

        
            let rigibody = this.node.getComponent(cc.RigidBody);
            // rigibody.active = true;
            rigibody.gravityScale = 0;

            //修改
            rigibody.linearVelocity = cc.v2(0,0);
            let len = 100*dt;
            let toppos = $T.getAnglePos( this.node.getPosition() , rotation , len);
            this.node.setPosition( toppos );
            
        //     var dx = 1200 * Math.cos( $T.toRota(rotation) );
        //     var dy = 1200 * Math.sin( $T.toRota(rotation) );
        // //cc.Vec2
        //     let posss = cc.v2(dx,dy);//e.touch.getLocation();
        //     let velocity =  posss.normalize().mulSelf( 120 );
        //     rigibody.linearVelocity = velocity;
        }
        // setTimeout(() => {
        //    this.node.isValid && this.unuse() &&  $G.do('poolPutByNode' , 'put' , this.node );
        // }, 500);

        return this;

    },
});
 